﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

using SisCRA.Config;

namespace SisCRA.Forms
{
    public partial class FormInitial : Form
    {
        #region -Constructor-

        /// <summary>
        /// Construtor padrão.
        /// </summary>
        public FormInitial()
        {
            InitializeComponent();
            LoadInformation();
            LoadComboBoxTeacherEmail();
        }

        #endregion

        #region -Events-

        private void buttonRegister_Click(object sender, EventArgs e)
        {
            string teacherName = textBoxTeacherName.Text;
            string teacherEmail = textBoxTeacherEmail.Text + comboBoxTeacherEmail.SelectedItem;
            string teacherEmailPassword = textBoxTeacherEmailPassword.Text;

            string educationalSectorName = textBoxEducationalSectorName.Text;
            string educationalSectorEmail = textBoxEducationalSectorEmail.Text;

            string path = Application.ExecutablePath;

            try
            {
                Regex regex = new Regex(InitialConfig.EmailRegex);

                if (string.IsNullOrWhiteSpace(teacherName) || string.IsNullOrWhiteSpace(teacherEmail) || string.IsNullOrWhiteSpace(teacherEmailPassword) || 
                    string.IsNullOrWhiteSpace(educationalSectorName) || string.IsNullOrWhiteSpace(educationalSectorEmail))
                {
                    MessageBox.Show(this, "Existem campos inválidos!", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else if (!regex.IsMatch(teacherEmail))
                {
                    textBoxTeacherEmail.Focus();
                    MessageBox.Show(this, "E-mail de registro do professor inválido!", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else if (!regex.IsMatch(educationalSectorEmail))
                {
                    textBoxEducationalSectorEmail.Focus();
                    MessageBox.Show(this, "E-mail de registro do setor pedagógico inválido!", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    InitialConfig.MakeInitialConfig(teacherName, teacherEmail, teacherEmailPassword, educationalSectorName, educationalSectorEmail, path);
                    this.Dispose();
                }
            }
            catch (Exception ex)
            {
                Log.Instance.Error(ex);
                MessageBox.Show(this, "Falha ao registrar usuário.", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        #endregion

        #region -Private Methods-

        /// <summary>
        /// Carrega as informações iniciais.
        /// </summary>
        private void LoadInformation()
        {
            this.textBoxInformation.Text = InitialConfig.Information();
        }

        /// <summary>
        /// Carrega o comboBoxTeacherEmail com o primeiro item da lista.
        /// </summary>
        private void LoadComboBoxTeacherEmail()
        {
            this.comboBoxTeacherEmail.SelectedIndex = 0;
        }

        #endregion
    }
}
